package com.etooling.framework.controller;

import com.etooling.framework.entity.Employee;
import com.etooling.framework.entity.Tag;
import com.etooling.framework.repository.EmployeeRepository;
import com.etooling.framework.repository.TagRepository;
import com.etooling.framework.svo.ReplyObj;
import com.etooling.framework.utils.WebUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Controller
@EnableAutoConfiguration
@RequestMapping(value = "/admin/tag")
public class TagController {
    @Autowired
    TagRepository tagRepository;

    //异步获取数据
    @RequestMapping("/getTagTree")
    @ResponseBody
    ReplyObj getTagTree(HttpServletRequest request,
                        @RequestParam(defaultValue = "", required = false, value = "target") List<String> targetList,
                        @RequestParam(defaultValue = "公司", required = false, value = "shearType") String shearType,
                        @RequestParam(defaultValue = "", required = false, value = "rootName") String rootName
    ) {
        try {
            List<Tag> resultList = new ArrayList<>();
            String companyId = (String) WebUtils.getSessionValue(request, WebUtils.LOGIN_COMPANY_ID_KEY);
            String loginUserId = (String) WebUtils.getSessionValue(request, WebUtils.LOGIN_USER_KEY);
            List<Tag> children = tagRepository.getChildren(targetList, shearType, companyId, loginUserId);
            if (CollectionUtils.isNotEmpty(children)) {
                Map<String, List<Tag>> groupByMap = children.stream()
                        .collect(Collectors.groupingBy(lt -> lt.getParentId()));
                resultList = this.getTree(groupByMap, "root");
            }
            return WebUtils.flushSuccess(resultList);
        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure("数据异常");
        }
    }

    public List<Tag> getTree(Map<String, List<Tag>> allNodeMap, String parentId) {
        List<Tag> list = allNodeMap.get(parentId);
        List<Tag> allTree = new ArrayList<>();
        list = allNodeMap.get(parentId);
        if (list != null && list.size() > 0) {
            for (Tag item : list) {
                List<Tag> children = null;
                if (allNodeMap.containsKey(item.getId())) {
                    children = getTree(allNodeMap, item.getId());
                }
                if (CollectionUtils.isNotEmpty(children)) {
                    item.setChildren(children);
                }
                allTree.add(item);
            }
        }
        return allTree;
    }

    @Autowired
    EmployeeRepository employeeRepository;

    //异步获取数据
    @RequestMapping("/add")
    @ResponseBody
    ReplyObj add(HttpServletRequest request,
                 @RequestParam(defaultValue = "", required = false, value = "target") String target,
                 @RequestParam(defaultValue = "", required = false, value = "tagColor") String tagColor,
                 @RequestParam(defaultValue = "", required = false, value = "shearType") String shearType,
                 @RequestParam(defaultValue = "", required = false, value = "tagId") String parentId,
                 @RequestParam(defaultValue = "", required = false, value = "aliasName") String aliasName,
                 @RequestParam(defaultValue = "", required = false, value = "name") String name,
                 @RequestParam(defaultValue = "是", required = false, value = "isQc") String isQc
    ) {
        try {
            String companyId = (String) WebUtils.getSessionValue(request, WebUtils.LOGIN_COMPANY_ID_KEY);
            String userId = (String) WebUtils.getSessionValue(request, WebUtils.LOGIN_USER_KEY);
            Employee loginUser = employeeRepository.getOne(userId);
            if (WebUtils.checkStr(target)) {
                Tag tag = new Tag();
                if (WebUtils.checkStr(shearType)) {
                    tag.setShearType(shearType);
                } else {
                    tag.setShearType("个人");
                }
                if (WebUtils.checkStr(tagColor)) {
                    tag.setTagColor(tagColor);
                }
                tag.setName(name);
                tag.setAliasName(aliasName);
                tag.setCompanyId(companyId);
                tag.setTarget(target);
                tag.setCreateUserId(userId);
                tag.setCreateUserName(loginUser.getName());
                tag.setStatus("启用");
                tag.setParentId(parentId);
                tag.setIsQc(isQc);
                tagRepository.save(tag);
                return WebUtils.flushSuccess(tag);
            } else {
                return WebUtils.flushFailure("数据异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure("数据异常");
        }
    }

    @RequestMapping("/edit")
    @ResponseBody
    ReplyObj edit(HttpServletRequest request,
                  @RequestParam(defaultValue = "", required = false, value = "name") String name,
                  @RequestParam(defaultValue = "", required = false, value = "aliasName") String aliasName,
                  @RequestParam(defaultValue = "", required = false, value = "tagId") String tagId,
                  @RequestParam(defaultValue = "", required = false, value = "tagColor") String tagColor,
                  @RequestParam(defaultValue = "", required = false, value = "isQc") String isQc
    ) {
        try {
            if (WebUtils.checkStr(tagId)) {
                Tag tag = tagRepository.getOne(tagId);
                tag.setName(name);
                if (WebUtils.checkStr(tagColor)) {
                    tag.setTagColor(tagColor);
                }
                tag.setAliasName(aliasName);
                tag.setIsQc(isQc);
                tagRepository.save(tag);
                return WebUtils.flushSuccess(tag);
            } else {
                return WebUtils.flushFailure("数据异常");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure("数据异常");
        }
    }

    @RequestMapping("/del")
    @ResponseBody
    ReplyObj del(HttpServletRequest request,
                 @RequestParam(defaultValue = "", required = false, value = "tagId") String tagId
    ) {
        try {
            if (WebUtils.checkStr(tagId)) {
                tagRepository.deleteById(tagId);
                return WebUtils.flushSuccess();
            } else {
                return WebUtils.flushFailure("数据异常");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure("数据异常");
        }
    }


}
